//
//  RecommendViewController.m
//  ProjectForA
//
//  Created by hmengwei on 14/11/19.
//  Copyright (c) 2014年 hmengwei. All rights reserved.
//

#import "RecommendViewController.h"
#import "HotView.h"
#import "ImageView.h"
#import "BargainsView.h"
#import "AFHTTPRequestOperationManager.h"
#import "RecommendModel.h"
#import "UIImageView+WebCache.h"
#import "TopDetailViewController.h"
#import "HotCell.h"
@interface RecommendViewController ()<UIScrollViewDelegate>

@end

@implementation RecommendViewController

#pragma mark -
#pragma mark 内存管理
- (void)dealloc
{
    [_bargainsArr release];
    [_travelArr release];
    [_imageArr release];
    [_hotArr release];
    [_modelArr release];
    [_imagVie release];
    [_hotView release];
    [_topPageControl release];
    [_topScroll release];
    [_recommendScroll release];
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr = [NSMutableArray array];
        self.hotArr = [NSMutableArray array];
        self.imageArr = [NSMutableArray array];
        self.travelArr = [NSMutableArray array];
        self.bargainsArr = [NSMutableArray array];

        
        
        // 第二种方法(tableview赋值)
//        self.hotView = [[HotView alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height / 3, self.view.frame.size.width - 20, 1000)];

        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"breadtrip";
    [self createrScrollView];

    [self createrAFNetworking];
    
    // 调用UIScrollView

}
#pragma mark -
#pragma mark 网络请求
- (void)createrAFNetworking
{
    
    
    NSString *gDetail =[NSString stringWithFormat:@"http://api.breadtrip.com/featured/"];
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil]];
    [manager GET:gDetail parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        //遍历整个数组
//        NSInteger count = [responseObject[@"elements"] count];
        NSArray *array = [responseObject objectForKey:@"elements"];
        // topScrollView赋值
        NSDictionary * dic = array.firstObject;
        NSArray * data = dic[@"data"];
        NSArray * imageArr = data.firstObject;
        for (NSDictionary * imageDic in imageArr) {
            RecommendModel *model = [[RecommendModel alloc] initWithDictionary:imageDic];
            [self.modelArr addObject:model];
            [model release];
        }
        // 最新热门游记赋值
        NSDictionary *hotDic = array[1];
        NSArray *hotData = hotDic[@"data"];
        for (NSDictionary *dic in hotData) {
//            NSLog(@"%@", dic[@"cover_image"]);
            RecommendModel *modelAr = [[RecommendModel alloc] initWithDictionary:dic];
//            //KVC
//            [modelAr setValuesForKeysWithDictionary:dic];
            // 第二种方法(tableview赋值)
//            [self.hotView.hotArray addObject:modelAr];
//            [self.hotView.tableView reloadData];
            [self.hotArr addObject:modelAr];
            [modelAr release];
            
        }
        //精美图片
        NSDictionary *imageDic = array[2];
        NSArray *imageData = [imageDic objectForKey:@"data"];
        for (NSDictionary * dic in imageData) {
            RecommendModel *modelAR = [[RecommendModel alloc] initWithDictionary:dic];
            [self.imageArr addObject:modelAR];
            [modelAR release];
        }
        //今日旅行专题
        NSDictionary *travelDic = [array objectAtIndex:4];
        NSArray *travelData = travelDic[@"data"];
        for (NSDictionary * dic in travelData) {
            RecommendModel *modelTrave = [[RecommendModel alloc] initWithDictionary:dic];
            [self.travelArr addObject:modelTrave];
            [modelTrave release];
        }
        //今天特价抢购
        NSDictionary *bargainsDic = array[5];
        NSArray *bargainsData = bargainsDic[@"data"];
        for (NSDictionary *dic in bargainsData) {
            RecommendModel *modelBains = [[RecommendModel alloc] initWithDictionary:dic];
            [self.bargainsArr addObject:modelBains];
            NSLog(@"%@", self.bargainsArr);
            [modelBains release];
        }
        
        [self createrTopScrollView];
        [self createrPageControl];
        [self createrHot];
        [self createrImage:self.imageArr];
        [self createrTravel];
        [self createrBargains:self.bargainsArr];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    

}

#pragma mark -
#pragma mark ScrollView
- (void)createrScrollView
{
    self.recommendScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49)];
    _recommendScroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_recommendScroll];
    [_recommendScroll release];
    _recommendScroll.contentSize = CGSizeMake(0, self.view.frame.size.height * 3.9 + 20);
}

#pragma mark - 
#pragma mark TopScrollView
- (void)createrTopScrollView
{
    self.topScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3)];
    [self.recommendScroll addSubview:_topScroll];
    _topScroll.delegate = self;
    _topScroll.contentSize = CGSizeMake(self.view.frame.size.width * self.modelArr.count, 0);
    _topScroll.pagingEnabled = YES;
    _topScroll.showsHorizontalScrollIndicator = NO;
    [_topScroll release];
    
    for (int i = 0; i < self.modelArr.count; i ++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height / 3)];
        [image setUserInteractionEnabled:YES];
        RecommendModel *model = [_modelArr objectAtIndex:i];
        [image setImageWithURL:[NSURL URLWithString:model.image_url]];
        [self.topScroll addSubview:image];
        
    }
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame = self.view.bounds;
//    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//    [self.topScroll addSubview:button];

}
- (void)buttonClicked:(UIButton *)button
{
    TopDetailViewController *topVC = [[TopDetailViewController alloc] init];
    [self.navigationController pushViewController:topVC animated:YES];
    [topVC release];
}



//  只要scrollview一滚动, 就调用这个方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //  把page和scroll绑定
    if (scrollView == _topScroll) {
        // 计算当前第几页
        int page = scrollView.contentOffset.x / scrollView.frame.size.width ;
        // 找到pagecontrol
        // 吧当前的页数给pagecontrol显示
        _topPageControl.currentPage = page;
        
    }
}

#pragma mark -
#pragma mark PageControl
- (void)createrPageControl
{
    self.topPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 6 + 60, self.view.frame.size.width, 40)];
    _topPageControl.numberOfPages = self.modelArr.count;
    [self.recommendScroll addSubview:_topPageControl];
    [_topPageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [_topPageControl release];
}

- (void)pageAction:(UIPageControl *)page
{
    // 通过调整scrollview的偏移量, 让scrollview调整位置
    _topScroll.contentOffset = CGPointMake(self.topScroll.frame.size.width * _topPageControl.currentPage, 0);
}

#pragma mark -
#pragma mark 最新热门游记

- (void)createrHot
{
    // 最新热门游记左边图片
    UIImageView *hotLeftImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.topScroll.frame.origin.y + self.topScroll.frame.size.height + 20, 8, 20)];
    hotLeftImage.image = [UIImage imageNamed:@"dest_title_rect@2x.png"];
    [_recommendScroll addSubview:hotLeftImage];
    [hotLeftImage release];
    
    // 内容
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, self.topScroll.frame.origin.y + self.topScroll.frame.size.height + 20, self.view.frame.size.width - 50, 20)];
    title.text =@"最新热门游记";
    [_recommendScroll addSubview:title];
    [title release];
    
    // 右边图片
    UIImageView *hotRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(title.frame.size.width + 20, self.topScroll.frame.origin.y + self.topScroll.frame.size.height + 20 , 20, 20) ];
    hotRightImage.image = [UIImage imageNamed:@"dest_title_arrow@2x.png"];
    [_recommendScroll addSubview:hotRightImage];
    [hotRightImage release];
    
    
    // 自定义tableview
    // 第二种方法(tableview赋值)
//    [_recommendScroll addSubview:_hotView];

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10,title.frame.origin.y +title.frame.size.height + 5 , self.view.frame.size.width - 20, 1000) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[HotCell class] forCellReuseIdentifier:@"reuse"];
    _tableView.scrollEnabled = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.recommendScroll addSubview:_tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _hotArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    RecommendModel *model = [_hotArr objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}



#pragma mark -
#pragma mark 每日精选图片

- (void)createrImage:(NSMutableArray *)arry
{
    // 每日精选图片左边图片
    UIImageView *hotLeftImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.tableView.frame.origin.y + self.tableView.frame.size.height, 8, 20)];
    hotLeftImage.image = [UIImage imageNamed:@"dest_title_rect@2x.png"];
    [_recommendScroll addSubview:hotLeftImage];
    [hotLeftImage release];
    
    // 内容
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, self.tableView.frame.origin.y + self.tableView.frame.size.height, self.view.frame.size.width - 50, 20)];
    title.text =@"每日精选图片";
    [_recommendScroll addSubview:title];
    [title release];
    
    // 右边图片
    UIImageView *hotRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(title.frame.size.width + 20, self.tableView.frame.origin.y + self.tableView.frame.size.height, 20, 20)];
    hotRightImage.image = [UIImage imageNamed:@"dest_title_arrow@2x.png"];
    [_recommendScroll addSubview:hotRightImage];
    [hotRightImage release];


    self.imagVie = [[ImageView alloc] initWithFrame:CGRectMake(10, self.tableView.frame.origin.y + self.tableView.frame.size.height + 25, self.view.frame.size.width - 20, 300)];
    self.imagVie.imageArray = arry;
    [_recommendScroll addSubview:_imagVie];
    [_imagVie release];
    
    
}
#pragma mark -
#pragma mark 今日旅行专题

- (void)createrTravel
{
    // 今日旅行专题左边图片
    UIImageView *hotLeftImage = [[UIImageView alloc] initWithFrame:CGRectMake(10,_imagVie.frame.origin.y + self.imagVie.frame.size.height + 20, 8, 20)];
    hotLeftImage.image = [UIImage imageNamed:@"dest_title_rect@2x.png"];
    [_recommendScroll addSubview:hotLeftImage];
    [hotLeftImage release];
    
    // 内容
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, _imagVie.frame.origin.y + self.imagVie.frame.size.height + 20, self.view.frame.size.width - 50, 20)];
    title.text =@"今日旅行专题";
    [_recommendScroll addSubview:title];
    [title release];
    
//    // 右边图片
    UIImageView *hotRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(title.frame.size.width + 20, _imagVie.frame.origin.y + self.imagVie.frame.size.height + 20, 20, 20)];
    hotRightImage.image = [UIImage imageNamed:@"dest_title_arrow@2x.png"];
    [_recommendScroll addSubview:hotRightImage];
    [hotRightImage release];

    
    UIImageView *travel = [[UIImageView alloc] initWithFrame:CGRectMake(10, title.frame.origin.y + title.frame.size.height + 10, self.view.frame.size.width - 20, 250)];
    RecommendModel *traModel = [_travelArr objectAtIndex:0];
    [travel setImageWithURL:[NSURL URLWithString:traModel.cover]];
    [_recommendScroll addSubview:travel];
    [travel release];
    

    
    self.travelTwo = [[UIImageView alloc] initWithFrame:CGRectMake(10, travel.frame.origin.y +travel.frame.size.height + 10, self.view.frame.size.width - 20, 250)];
    RecommendModel *traTwoModel = [_travelArr objectAtIndex:1];
    [_travelTwo setImageWithURL:[NSURL URLWithString:traTwoModel.cover]];
    [_recommendScroll addSubview:_travelTwo];
    [_travelTwo release];
    
}


#pragma mark -
#pragma mark 每日特价抢购

- (void)createrBargains:(NSMutableArray *)arr
{
    // 最新热门游记左边图片
    UIImageView *hotLeftImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.travelTwo.frame.origin.y + self.travelTwo.frame.size.height + 20, 8, 20)];
    hotLeftImage.image = [UIImage imageNamed:@"dest_title_rect@2x.png"];
    [_recommendScroll addSubview:hotLeftImage];
    [hotLeftImage release];
    
    // 内容
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, self.travelTwo.frame.origin.y + self.travelTwo.frame.size.height + 20, self.view.frame.size.width - 50, 20)];
    title.text =@"每日特价抢购";
    [_recommendScroll addSubview:title];
    [title release];
    
    // 右边图片
    UIImageView *hotRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(title.frame.size.width + 20, self.travelTwo.frame.origin.y + self.travelTwo.frame.size.height + 20, 20, 20)];
    hotRightImage.image = [UIImage imageNamed:@"dest_title_arrow@2x.png"];
    [_recommendScroll addSubview:hotRightImage];
    [hotRightImage release];
    
    
    // 自定义tableview
    self.bargainsView = [[BargainsView alloc] initWithFrame:CGRectMake(10, title.frame.origin.y +title.frame.size.height + 10, self.view.frame.size.width - 20, 450)];
    _bargainsView.backgroundColor = [UIColor orangeColor];
    _bargainsView.bargainsArray = arr;
    [_recommendScroll addSubview:_bargainsView];
    [_bargainsView release];

    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
